package com.cz.nongyetongv5.activity;

import java.io.File;
import java.util.UUID;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.custom.PaintView.PaintView;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;
import com.cz.nongyetongv5.utils.ImageUtil;

/**
 * 签名的Activity
 *
 * @author admin
 *
 */
public class QianMingActivity extends Activity {

	/**
	 * 手写板对象
	 */
	private PaintView pvWritePaint = null;

	/**
	 * 清除按钮
	 */
	private View btnClearWrite = null;

	/**
	 * 已经存在的签名的地址
	 */
	private String qianMingPath = null;

	/**
	 * 返回按钮
	 */
	private ViewGroup btnBack = null;

	/**
	 * 当返回的时候
	 */
	private void doBack() {

		// 如果是有内容的图片
		if (pvWritePaint.isHasContent()) {

			// 进行图片的保存
			File qianMingSaveDir = new File(ImageUtil.getStoragePath(),
					("nongyetong" + File.separator + "photo" + File.separator
							+ "qianming" + File.separator));

			// 创建签名文件路径
			ImageUtil.makeDir(qianMingSaveDir);

			String fileName = UUID.randomUUID().toString().replaceAll("-", "");

			// 签名文件名称
			fileName = fileName + ".jpg";

			// 签名文件
			File smallSavePhoto = new File(qianMingSaveDir, fileName);

			// 保存签名文件
			ImageUtil.bigmapSave(smallSavePhoto, pvWritePaint.getImage(), 10);

			// 设置文件
			qianMingPath = smallSavePhoto.getAbsolutePath();

			pvWritePaint.getImage().recycle();

			pvWritePaint = null;

			System.gc();

		} else {
			qianMingPath = null;
		}

		Intent intent = new Intent();

		intent.putExtra("qianmingpath", qianMingPath);

		setResult(JianDuJianChaNewRecordActivity.QIANMING_REQUEST_CODE, intent);

		finish();

	}

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.qianming);

		initView();

		initConfig();

	}

	private void initView() {
		// 加载画板
		pvWritePaint = (PaintView) this.findViewById(R.id.pvWritePaint);

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "清除", "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn1");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

		// 加载清除按钮
		btnClearWrite = (View) pop.getContentView().findViewWithTag("btn0");

		// 加载返回按钮
		btnBack = (ViewGroup) findViewById(R.id.btnBack);

		// 当返回的时候进行图片的保存
		btnBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doBack();
			}
		});

		// 绑定单机事件
		btnClearWrite.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				pvWritePaint.clear();
			}
		});
	}

	private void initConfig() {

		// 加载从上个页面传递过来的图片地址数据
		this.qianMingPath = getIntent().getStringExtra("qianmingpath");

		// 如果已经存在路径了
		if (CharsetUtil.isNotNull(this.qianMingPath)) {

			BitmapFactory.Options opts = new BitmapFactory.Options();

			// 复制一份,否则不允许直接编辑本地图片
			Bitmap iamge = BitmapFactory.decodeFile(this.qianMingPath, opts)
					.copy(Config.ARGB_8888, true);

			pvWritePaint.setImage(iamge);
		}

	}

	@Override
	public void onBackPressed() {
		doBack();
	}

}
